class Solution(object):
    def smallestSubsequence(self, s):
        d = {}
        for index, c in enumerate(s):
            d[c] = index
        out = []
        for index, c in enumerate(s):
            if c not in out:
                while out and out[-1] > c and d[out[-1]] > index:
                    out.pop()
                out.append(c)
        return ''.join(out)


data = Solution()
s = "bcabc"
print(data.smallestSubsequence(s))
